import { isNode as g } from '../nodes/identity.js';
import { createStringifyContext as v, stringify as h } from './stringify.js';
import { indentComment as r, lineComment as a } from './stringifyComment.js';
function S(t, c) {
  var u;
  const e = [];
  let f = c.directives === !0;
  if (c.directives !== !1 && t.directives) {
    const n = t.directives.toString(t);
    n ? (e.push(n), (f = !0)) : t.directives.docStart && (f = !0);
  }
  f && e.push('---');
  const m = v(t, c),
    { commentString: o } = m.options;
  if (t.commentBefore) {
    e.length !== 1 && e.unshift('');
    const n = o(t.commentBefore);
    e.unshift(r(n, ''));
  }
  let l = !1,
    s = null;
  if (t.contents) {
    if (g(t.contents)) {
      if (
        (t.contents.spaceBefore && f && e.push(''), t.contents.commentBefore)
      ) {
        const p = o(t.contents.commentBefore);
        e.push(r(p, ''));
      }
      (m.forceBlockIndent = !!t.comment), (s = t.contents.comment);
    }
    const n = s ? void 0 : () => (l = !0);
    let i = h(t.contents, m, () => (s = null), n);
    s && (i += a(i, '', o(s))),
      (i[0] === '|' || i[0] === '>') && e[e.length - 1] === '---'
        ? (e[e.length - 1] = `--- ${i}`)
        : e.push(i);
  } else e.push(h(t.contents, m));
  if ((u = t.directives) != null && u.docEnd)
    if (t.comment) {
      const n = o(t.comment);
      n.includes(`
`)
        ? (e.push('...'), e.push(r(n, '')))
        : e.push(`... ${n}`);
    } else e.push('...');
  else {
    let n = t.comment;
    n && l && (n = n.replace(/^\n+/, '')),
      n &&
        ((!l || s) && e[e.length - 1] !== '' && e.push(''),
        e.push(r(o(n), '')));
  }
  return (
    e.join(`
`) +
    `
`
  );
}
export { S as stringifyDocument };
